
package com.dcrzl.shcz.common.exception;

import com.dcrzl.shcz.common.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;


/**
 * 异常处理器
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestControllerAdvice
@Slf4j
public class MyExceptionHandler {
	/**
	 * 处理自定义异常
	 */
	@ExceptionHandler(MyException.class)
	public Result<String> handleRRException(MyException e){
		return new Result<String>().setSuccess(false).setMsg(e.getMessage()).setCode(e.getCode());
	}

	@ExceptionHandler(NoHandlerFoundException.class)
	public Result<String> handlerNoFoundException(Exception e) {
		log.error(e.getMessage(), e);
		return new Result<String>().setSuccess(false).setMsg("路径不存在，请检查路径是否正确")
				.setCode(404);
	}

	@ExceptionHandler(DuplicateKeyException.class)
	public Result<String> handleDuplicateKeyException(DuplicateKeyException e){
		log.error(e.getMessage(), e);
		return new Result<String>().setSuccess(false).setMsg("数据库中已存在该记录")
				.setCode(502);
	}

//	@ExceptionHandler(AuthorizationException.class)
//	public R handleAuthorizationException(AuthorizationException e){
//		logger.error(e.getMessage(), e);
//		return R.error("没有权限，请联系管理员授权");
//	}

	@ExceptionHandler(Exception.class)
	public Result<String> handleException(Exception e){
		log.error(e.getMessage(), e);
		return new Result<String>().setSuccess(false).setMsg(e.getMessage()).setCode(500);
	}
}
